package airthmetic.exercise.stack;


import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @Author douhy
 * @description 栈相关算法
 * @date 22/01/19
 */
public class StackDemo {

    private Queue inQueue;
    private Queue outQueue;

    public StackDemo() {
        inQueue = new LinkedList();
        outQueue = new LinkedList();
    }

    // 思路
    // 利用inQueue接收参数
    // 将之前的参数(outQueue)放到inQueue后面
    // 将inQueue弄个临时变量
    // 将inQueue拷贝到outQueue
    // 将inQueue清空
    public void push(int x) {
        inQueue.offer(x);
        while (!outQueue.isEmpty()){
            inQueue.offer(outQueue.poll());
        }
        Queue tempQueue = inQueue;
        inQueue = outQueue;
        outQueue = tempQueue;
    }

    public int pop() {
        return (int) outQueue.poll();
    }

    public int top() {
        return (int) outQueue.peek();
    }

    public boolean empty() {
        return outQueue.isEmpty();
    }

    @Override
    public String toString() {
        return "StackDemo{" +
                "inQueue=" + inQueue +
                ", outQueue=" + outQueue +
                '}';
    }

    // leetCode 20. 有效的括号
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        for (char c : s.toCharArray()) {
            if (c == '(') stack.push(')');
            else if (c == '[') stack.push(']');
            else if (c == '{') stack.push('}');
            else if (stack.isEmpty() || c != stack.pop()) return false;
        }
        return stack.isEmpty();
    }


    public static void main(String[] args) {
        StackDemo stack = new StackDemo();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(6);
        System.out.println(stack);
        stack.pop();
        System.out.println(stack);
        stack.pop();
        System.out.println(stack);
        stack.pop();
        System.out.println(stack);

//        System.out.println(isValid("(){()}}"));
    }
}
